require(['jquery',
         'global',
         'module/util',
         'bootbox.min',
         'module/ajax',
         'jquery.validate',
         'bootstrap',
         'pixeladmin'], 
function($, global, util, bootbox){
	window.PixelAdmin.start();
    //#J_permissionContainer
    //.J_roleList
    //.J_save

    $(document).on('change','.J_roleList',function(e){
        var self = $(e.currentTarget);
        if(self.val()=="0"){
            //
        }
        $.ajax({
            url: global.context+"/web/system/getRolePermissions",
            type: "get",
            cache:false,
            data: {
                roleId:$(e.currentTarget).val()
            },
            dataType: "json",
            success: function(result) {
                if(result){
                    if(result.code == "ACK" ||result.code == "ACK_NO_MESSAGE"){
                        var rolePermissions = result.data;
                        var permissionsContainer = $('#J_permissionContainer');
                        var checkboxs = permissionsContainer.find('input[type=checkbox]');
                        for(var i=0;i<checkboxs.length;i++){
                            checkboxs.eq(i)[0].checked=false;
                        }
                        if(rolePermissions!=null && rolePermissions.length>0){
                            for(var i=0;i<rolePermissions.length;i++){
                                var one = checkboxs.filter('input[permissionId="'+rolePermissions[i].permissionId+'"]');
                                one[0].checked=true;
                            }
                        }
                    }
                }
            }
        });
    });

    $(document).on('click','.J_save',function(e){
        var self = $(e.currentTarget);
        if(self.hasClass('disabled')){
            return;
        }
        var roleId = $('.J_roleList').val();
        if(roleId=="0"){
            $.growl.warning({ 
                title:"提示",
                message: "请选择角色。" 
            });
            return;
        }
        var permissions = $('#J_permissionContainer').find('input[type=checkbox]');
        var checked = permissions.filter(':checked');
        var grantedPermissions = [];
        if(checked.length>0){
            for(var i=0;i<checked.length;i++){
                grantedPermissions.push(checked.eq(i).attr('permissionId'));
            }
        }

        bootbox.confirm({
            buttons: {  
                confirm: {  
                    label: '确认',  
                    className: 'btn-myStyle'  
                },  
                cancel: {  
                    label: '取消',  
                    className: 'btn-default'  
                }  
             },  
            message: "确定要保存吗?",
            callback: function(isOk) {
                if(isOk){
                    $.ajax({
                        url: global.context+'/web/system/assignPermissions',
                        type: "POST",
                        cache:false,
                        data: JSON.stringify({
                            roleId:roleId,
                            permissions:grantedPermissions.join(',')
                        }),
                        beforeSend:function(){
                            self.addClass('disabled');
                        },
                        dataType: "json",
                        contentType: 'application/json;charset=utf-8',
                        error: function(request) {
                        },
                        success: function(result) {
                            if(result){
                                if(result.code == "ACK"||result.code == "ACK_NO_MESSAGE"){
                                    //var data = result.data;
                                }else{
                                }
                            }
                        },
                        complete:function(){
                            self.removeClass('disabled');
                        }
                    });
                }
            },
            className: "bootbox-sm"
        });
        
    });
});